package wqas

import (
	"bufio"
	"encoding/json"
	"fmt"
	"io"
	"log"
	"net/http"
	"os"
	"strconv"

	"github.com/sirupsen/logrus"
)

type StatistisHandler struct {
	statisticsCount int
}

func NewStatisticsHandler() *StatistisHandler {
	// 设置logrus的Formatter为textformatter，并开启显示函数名
	return &StatistisHandler{}
}

func (ctx *StatistisHandler) Sleep() int {
	return 0
	// r := rand.Intn(3) + 2
	// time.Sleep(time.Duration(r) * time.Second)
	// return r
}

func (ctx *StatistisHandler) GetStatistics(w http.ResponseWriter, r *http.Request) {
	logrus.Info("GetStatistics")
	if r.Method == "GET" {
		r.ParseForm()
		page, _ := strconv.Atoi(r.Form.Get("page"))
		pageSize, _ := strconv.Atoi(r.Form.Get("pageSize"))

		reportDate := r.Form.Get("reportDate")
		topDefect := r.Form.Get("topDefect")

		logrus.Info("page: ", page, " pageSize: ", pageSize, " reportDate: ", reportDate, " topDefect: ", topDefect)

		inputFile := "webserver/assets/wqas/statistics/getPassRate_en.json"

		inputReader, error := os.Open(inputFile)
		if error != nil {
			logrus.Error(error)
		} else {
			inputReader := bufio.NewReader(inputReader)
			w.Header().Set("Content-Type", "application/json")
			r.ParseForm()
			io.Copy(w, inputReader)
		}
	}
}

/* 合格率统计 */
func (ctx *StatistisHandler) GetPassRate(w http.ResponseWriter, r *http.Request) {
	if r.Method == "GET" {
		r.ParseForm()
		page, _ := strconv.Atoi(r.Form.Get("page"))
		pageSize, _ := strconv.Atoi(r.Form.Get("pageSize"))

		reportDate := r.Form.Get("reportDate")
		topDefect := r.Form.Get("topDefect")

		logrus.Info("page: ", page, " pageSize: ", pageSize, " reportDate: ", reportDate, " topDefect: ", topDefect)

		inputFile := "webserver/assets/wqas/statistics/getPassRate_en.json"

		inputReader, error := os.Open(inputFile)
		if error != nil {
			logrus.Error(error)
		} else {
			inputReader := bufio.NewReader(inputReader)
			w.Header().Set("Content-Type", "application/json")
			r.ParseForm()
			io.Copy(w, inputReader)
		}
	}
}

/*
缺陷统计
*/
func (ctx *StatistisHandler) GetDefect(w http.ResponseWriter, r *http.Request) {
	t := ctx.Sleep()
	switch r.Method {
	case "GET":
		w.Header().Set("Content-Type", "application/json")
		// 写入 sessionid 的方式代替 token
		r.ParseForm()
		page, _ := strconv.Atoi(r.Form.Get("page"))
		pageSize, _ := strconv.Atoi(r.Form.Get("pageSize"))

		reportDate := r.Form.Get("reportDate")
		topDefect := r.Form.Get("topDefect")

		logrus.Info("page: ", page, " pageSize: ", pageSize, " reportDate: ", reportDate, " topDefect: ", topDefect)

		inputFile := "webserver/assets/wqas/statistics/getDefect_en.json"

		inputReader, error := os.Open(inputFile)
		if error != nil {
			logrus.Error(error)
		} else {
			inputReader := bufio.NewReader(inputReader)
			w.Header().Set("Content-Type", "application/json")
			r.ParseForm()
			io.Copy(w, inputReader)
		}
	case "POST":
		ip := r.FormValue("ip")

		var response = make(map[string]interface{})
		if len(ip) > 0 {
			response["code"] = 200
			response["msg"] = "机械臂未连接"
			response["data"] = map[string]string{"msg": "connect success", "rand": strconv.Itoa(t), "status": "0"}
		} else {
			response["code"] = -1
			response["msg"] = "failure"
			response["data"] = map[string]string{"msg": "The IP cannot be empty", "rand": strconv.Itoa(t), "status": "-1"}
		}

		jsonByte, _ := json.Marshal(response)
		str := string(jsonByte)
		log.Println("res: ", str)
		fmt.Println("res: ", str)
		io.WriteString(w, str)
	}
}
